<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="app"></div>
  <script>
    // vue2
    var obj = {
      a: 100,
      b: 200
    }
   /*  var a = 100
    // 劫持对象a属性
    Object.defineProperty(obj, 'a', {
      // getter函数
      get(){
        console.log('getter触发');
        return a
      },
      // setter函数
      set(val){
        console.log('setter触发', val);
        a = val;
      }
    })
    console.log(obj.a);
    obj.a = 500;
    console.log(obj.a); */
    // 代理
    var objProxy = new Proxy(obj, {
      // getter
      get(target, key){
        console.log('getter触发');
        return target[key];
      },
      // setter
      set(target, key, val){
        console.log('setter触发', val);
        target[key] = val;
      }
    })
    console.log(objProxy.a);
    objProxy.a = 5000;
    console.log(objProxy.a);
  </script>
</body>
</html>